package kr.hkit.summary_java.erp.serviceui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.uicomp.InputComboPanel;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.erp.dao.CodeTitleDao;
import kr.hkit.summary_java.erp.dao.DaoInterface;
import kr.hkit.summary_java.erp.dao.DepartmentDao;
import kr.hkit.summary_java.erp.dao.EmployeeDao;
import kr.hkit.summary_java.erp.dto.CodeTitle;
import kr.hkit.summary_java.erp.dto.Department;
import kr.hkit.summary_java.erp.dto.Employee;

public class PanelEmployeeUI extends JPanel implements ActionListener {
	private InputPanel pEmpNo;
	private InputPanel pEmpName;
	private InputPanel pManager;
	private InputPanel pSalary;
	private InputComboPanel pDno;
	private InputComboPanel pTitle;
	private JPanel panel;
	private JButton btn;
	
	
	private Connection con = ConnectionFactory.getConnection();
	private DaoInterface<Employee> eDao = new EmployeeDao(con);
	private DaoInterface<Department> dDao = new DepartmentDao(con);
	private DaoInterface<CodeTitle> tDao = new CodeTitleDao(con);

	public void setBtn(String s) {
		this.btn.setText(s);
	}

	public void setDao(DaoInterface<Employee> dao) {
		this.eDao = dao;
	}

	/**
	 * Create the panel.
	 */
	public PanelEmployeeUI(String s) {
		initialize(s);
	}

	private void initialize(String s) {

		setLayout(new GridLayout(0, 1, 0, 0));

		panel = new JPanel();
		add(panel);
		panel.setLayout(new GridLayout(0, 1, 5, 3));

		pEmpNo = new InputPanel("사원번호");
		panel.add(pEmpNo);
		pEmpNo.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				Employee emp = null;
				
				try {
					emp = eDao.searchItem(new Employee(Integer.parseInt(pEmpNo
							.getTf().getText().trim())));
					if (emp != null) {
						pEmpNo.setTf(emp.getEmpNo() + "");
						pEmpName.setTf(emp.getEmpName() + "");
						pTitle.setCb(ChangeCb(emp.getKor(), "t"));
						pManager.setTf(emp.getEng() + "");
						pSalary.setTf(emp.getMath() + "");
						pDno.setCb(ChangeCb(emp.getDeptNo(), "d"));
						if (btn.getText() == "검색") {
							panelEditable(false);
						}
					} else if (btn.getText() != "추가") {
						JOptionPane.showMessageDialog(PanelEmployeeUI.this,
								"없는 정보");
						clsPanel();
					}
				} catch (NumberFormatException | SQLException e1) {
					e1.printStackTrace();
				}
			}
		});

		pEmpName = new InputPanel("사원명");
		panel.add(pEmpName);

		ArrayList<CodeTitle> cList = new ArrayList<>();
		try {
			cList = tDao.listItems();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		ArrayList<String> list = new ArrayList<>();
		for (int i = 0; i < cList.size(); i++) {
			list.add(cList.get(i).getTitleName());
		}
		pTitle = new InputComboPanel("직급", list);
		panel.add(pTitle);

		pManager = new InputPanel("직속상관");
		panel.add(pManager);

		pSalary = new InputPanel("월급");
		panel.add(pSalary);

		ArrayList<Department> cList2 = new ArrayList<>();
		try {
			cList2 = dDao.listItems();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		ArrayList<String> list2 = new ArrayList<>();
		for (int i = 0; i < cList2.size(); i++) {
			list2.add(cList2.get(i).getDeptName());
		}
		pDno = new InputComboPanel("부서번호", list2);
		panel.add(pDno);

		btn = new JButton();
		panel.add(btn);
		btn.addActionListener(this);
		setBtn(s);
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn) {
			try {
				actionPerformedBtn(e);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}

	protected void actionPerformedBtn(ActionEvent e) throws SQLException {
		int empNo;
		String empName;
		int title;
		int manager;
		int salary;
		int dno;
		Employee emp = null;
		try {
			switch (btn.getText()) {
				case "추가":
					empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
					empName = pEmpName.getTf().getText();
					title = inputCb("titleno", "codetitle", "titlename", (String)pTitle.getCb().getSelectedItem());
					manager = inputManager(pManager.getTf().getText());
					salary = Integer.parseInt(pSalary.getTf().getText().trim());
					dno = inputCb("deptno", "department", "deptname", (String)pDno.getCb().getSelectedItem());
					emp = new Employee(empNo, empName, title, manager, salary,
							dno);
					eDao.insertItem(emp);
					JOptionPane.showMessageDialog(this, "추가 성공");
					clsPanel();
					break;
				case "삭제":
					empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
					emp = new Employee(empNo);
					eDao.deleteItemByNo(emp);
					JOptionPane.showMessageDialog(this, "삭제 성공");
					pEmpNo.setTf("");
					clsPanel();
					break;
				case "수정":
					empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
					empName = pEmpName.getTf().getText();
					title = inputCb("titleno", "codetitle", "titlename", (String)pTitle.getCb().getSelectedItem());
					manager = inputManager(pManager.getTf().getText());
					salary = Integer.parseInt(pSalary.getTf().getText().trim());
					dno = inputCb("deptno", "department", "deptname", (String)pDno.getCb().getSelectedItem());
					emp = new Employee(empNo, empName, title, manager, salary,
							dno);
					eDao.updateItemByNo(emp);
					JOptionPane.showMessageDialog(this, "수정 성공");
					clsPanel();
					break;
				case "검색":
					empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
					emp = eDao.searchItem(new Employee(empNo));
					if (emp != null) {
						pEmpNo.setTf(emp.getEmpNo() + "");
						pEmpName.setTf(emp.getEmpName() + "");
						pTitle.setCb(ChangeCb(emp.getKor(), "t"));
						pManager.setTf(emp.getEng() + "");
						pSalary.setTf(emp.getMath() + "");
						pDno.setCb(ChangeCb(emp.getDeptNo(), "d"));
					} else {
						JOptionPane.showMessageDialog(this, "없는 정보");
					}
					break;
			}
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(this, "다시 입력하세요");
			clsPanel();
		}
	}

	private String ChangeCb(int titleNo, String res) throws SQLException {
		String sql = null;
		switch (res) {
			case "t":
				sql = "select titlename from codetitle where titleno = ?";
				break;
			case "d":
				sql = "select deptname from department where deptno = ?";
				break;
		}
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, titleNo);
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			return rs.getString(1);
		} else {
			return null;
		}
	}

	private int inputManager(String s) throws SQLException {
		String sql = "select empno from employee where empname = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, s);
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			return rs.getInt(1);
		} else {
			return -1;
		}
	}

	private int inputCb(String s1, String s2, String s3, String s4) throws SQLException {
		String sql = "select " + s1 + " from " + s2 + " where " + s3 + " = ? ";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, s4);
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			return rs.getInt(1);
		} else {
			return -1;
		}
	}
	
	private void panelEditable(boolean set) {
		if (set) {
			pEmpNo.getTf().setEditable(true);
			pEmpName.getTf().setEditable(true);
			pTitle.getCb().setEditable(true);
			pManager.getTf().setEditable(true);
			pSalary.getTf().setEditable(true);
			pDno.getCb().setEditable(true);
		} else {
			pEmpNo.getTf().setEditable(false);
			pEmpName.getTf().setEditable(false);
			pTitle.getCb().setEnabled(false);
			pManager.getTf().setEditable(false);
			pSalary.getTf().setEditable(false);
			pDno.getCb().setEnabled(false);
		}
	}

	private void clsPanel() {
		pEmpNo.setTf("");
		pEmpName.setTf("");
		pTitle.setCb(-1);
		pManager.setTf("");
		pSalary.setTf("");
		pDno.setCb(-1);
	}
}
